﻿using System;
using System.IO;
using System.Runtime.Serialization.Json;
using Petra.Gis.Geometry.Support;
using Petra.Gis.ReferenceSystemServices;
using Petra.Gis.ReferenceSystemServices.Transformations;


namespace testSRST
{
  public class testJsonSRS
  {


    public static void testMe()
    {

      //string outstr;


      // test of search coord system
      //CoordinateSystem csSearch = CoordinateSystem.SearchBy("INGR", "49", "milimetre");

      CoordinateSystem cs;
      ProjectedCoordinateSystem pcs;
      GeographicCoordinateSystem gcs;

      MemoryStream m1 = new MemoryStream();
      MemoryStream m2 = new MemoryStream();

      DataContractJsonSerializer j1 = new DataContractJsonSerializer(typeof(ProjectedCoordinateSystem));
      DataContractJsonSerializer j2 = new DataContractJsonSerializer(typeof(GeographicCoordinateSystem));

      gcs = testSRSJtsk.createGeogCS_krovak();
      pcs = testSRSJtsk.createJTSK_49();

      j1.WriteObject(m1, pcs);
      j2.WriteObject(m2, gcs);

      //1.test directly from stream
      m1.Position = 0;
      ProjectedCoordinateSystem pcsOut = (ProjectedCoordinateSystem)j1.ReadObject(m1);

      //2.test from string
      String crsc = System.Text.Encoding.Default.GetString(m2.ToArray());
      m2.Position = 0; 
      System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
      byte[] bo = encoding.GetBytes(crsc);
      MemoryStream mOut = new MemoryStream(bo);
      GeographicCoordinateSystem gcsOut = (GeographicCoordinateSystem)j2.ReadObject(mOut);

      //3.to/from file
      //string jsonPath = "D:\\My Documents\\C#.NET\\Petra.Gis\\ReferenceSystemsServices\\testSRST\\bin\\Debug\\CoordinateSystemCache.json";
      //FileStream fs = new FileStream(jsonPath, FileMode.Create, FileAccess.ReadWrite);
      //fs.Write(encoding.GetBytes(crsc), 0, bo.Length);
      //FileStream fs = new FileStream(jsonPath, FileMode.Open, FileAccess.Read);

      //fs.Position = 0;
      
      //fs.Close();
    }


  }
}
